<script>
	import {
		mapActions,
		mapGetters,
		mapMutations
	} from "vuex";
	import {
		apiDistributionCode
	} from "@/api/user";
	import {
		apiCitySupplierBindCode,
		apiCitySupplierBindUser
	} from "@/api/citysupplier"
	import {
		strToParams
	} from "@/utils/tools";
	import {
		INVITE_CODE,
	} from "@/config/cachekey";
	import {
		apiCloudShopBindCode,
	} from "@/api/cloudshop"
	import {
		apiLang,
	} from "@/api/app"
	import Cache from "@/utils/cache";
	import {
		zhTran
	} from './utils/langs'
	export default {
		async onLaunch(options) {

			// #ifdef MP-WEIXIN
			const updateManager = uni.getUpdateManager()
			if (updateManager) {
				if (uni.canIUse('getUpdateManager')) {

					updateManager.onCheckForUpdate(function(res) {
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							updateManager.onUpdateReady(function(res1) {
								uni.showModal({
									title: zhTran('新版更新提示'),
									content: zhTran('新版本已经准备好，是否重启应用？'),
									success(res2) {
										if (res2.confirm) {
											// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
											updateManager.applyUpdate()
										}
									}
								})
							})
						}
					})

					updateManager.onUpdateFailed(function(res) {
						// 新版本下载失败
						uni.showModal({
							title: zhTran('新版更新提示'),
							content: zhTran('下载失败，请您移除本小程序，重新搜索进入'),
						})
					})
				} else {
					uni.showModal({
						// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
						title: zhTran('新版更新提示'),
						content: zhTran('当前微信版本过低，部分功能无法使用，请升级到最新微信版本后重试。'),
					});
				}
			}
			// #endif

			let cloud_shop_enabled = 0,
				visitor_login_verification = 0
			// 获取公共配置
			await this.getConfig().then((res) => {
				cloud_shop_enabled = res.cloud_shop_enabled
				visitor_login_verification = res.visitor_login_verification
				Cache.set('visitor_login_verification', visitor_login_verification);
				// 当后台配置商城停止后相关商城进行跳转到空页面
				// #ifdef H5
				if (!res.h5_shop_enabled) {
					setTimeout(() => {
						uni.redirectTo({
							url: "/bundle2/pages/index/close_shop",
						});
					}, 0);
				} else if (!res.h5_status) {
					setTimeout(() => {
						uni.redirectTo({
							url: "/bundle/pages/business_suspended/business_suspended",
						});
					}, 0);
				} else {
					if (options.path == "pages/index/close_shop")
						setTimeout(() => {
							uni.redirectTo({
								url: "/",
							});
						}, 0);
				}
				// #endif
				// #ifdef MP-WEIXIN
				if (!res.mnp_status) {
					setTimeout(() => {
						uni.redirectTo({
							url: "/bundle/pages/business_suspended/business_suspended",
						});
					}, 0);
				}
				// #endif
			});
			// 获取主题配置
			await this.getDecorateConfig();
			// 获取个人信息
			var that = this
			this.getUser().then((res) => {
				// 更新购物车信息
				this.$store.dispatch("getCartNum");
				if (this.bindMobile && !res.mobile) {
					this.logout();
				}
			}).catch(() => {
				if (visitor_login_verification) {
					uni.showModal({
						content: that.$zhTran('由于本系统仅限指定客户查看价格和进行下单，请登陆后查看'),
						confirmColor: that.themeColor,
						confirmText: that.$zhTran('确认'),
						showCancel: false,
						complete() {
							that.$Router.replaceAll({
								path: '/bundle2/pages/login/login',
							})
						}
					})
				}
			});


			Cache.set('cloud_shop_enabled', cloud_shop_enabled, 24 * 60 * 60);
			if (cloud_shop_enabled) {
				if (this.$Route.path != '/bundle2/pages/goods_detail/goods_detail')
					this.$Router.replaceAll({
						path: '/bundle2/pages/cloud_shop/diyshop/index',
					})
			} else {
				Cache.remove('clsp_id');
			}
		},
		onShow(options) {
			this.bindCode(options);
			this.bindCspplCode(options);
			this.bindCspplPullUserCode(options);
			this.bindClspCode(options);

			if (this.cloud_shop_enabled) wx.hideHomeButton()
			// console.log('App Show')
		},
		onHide: function() {
			console.log("App Hide");
		},
		computed: {
			bindMobile() {
				// 强制绑定手机号
				return this.appConfig.coerce_mobile;
			},
		},
		methods: {
			...mapActions(["getConfig", "getDecorateConfig", "getUser"]),
			...mapMutations(["logout"]),
			async bindCode(options) {
				if (!options.query) return;
				let invite_code =
					options.query.invite_code ||
					strToParams(decodeURIComponent(options.query.scene)).invite_code;
				console.log(options);
				if (invite_code) {
					apiDistributionCode({
						code: invite_code,
						hide: 1,
					}).catch(() => {
						Cache.set(INVITE_CODE, invite_code);
					});
				}
			},
			async bindCspplCode(options) {
				if (!options.query) return;
				let cspplId =
					options.query.cspplId ||
					strToParams(decodeURIComponent(options.query.scene)).cspplId,
					cspplSale = options.query.cspplSale ||
					strToParams(decodeURIComponent(options.query.scene)).cspplSale
				console.log(options);
				if (cspplId && cspplSale) {
					apiCitySupplierBindCode({
						csppl_id: cspplId,
						csppl_sale: cspplSale
					}).catch(() => {
						Cache.set('csppl_id', cspplId);
						Cache.set('csppl_sale', cspplSale);
					});
				}
			},
			async bindCspplPullUserCode(options) {
				if (!options.query) return;
				let cspplSale = options.query.cspplSale ||
					strToParams(decodeURIComponent(options.query.scene)).cspplSale,
					PType = options.query.PType ||
					strToParams(decodeURIComponent(options.query.scene)).PType,
					PLevel = options.query.PLevel ||
					strToParams(decodeURIComponent(options.query.scene)).PLevel

				if (cspplSale && PType != undefined && PLevel != undefined) {
					this.getUser().then((res) => {
						if (PType == 1 && res.cgp_leader != 1) {
							this.$Router.push({
								path: '/bundle2/pages/community_group_purchase/leader/leader_center',
								query: {
									share_id: cspplSale,
								}
							})
						} else if (res.level != PLevel) {
							this.$Router.push({
								path: '/bundle2/pages/level_apply/apply',
								query: {
									share_id: cspplSale,
									poster_level: PLevel
								}
							})
						} else {
							apiCitySupplierBindUser({
								csppl_sale: cspplSale
							});
						}
					}).catch(() => {
						this.$Router.push({
							path: "/bundle2/pages/login/login",
							query: {
								share_id: cspplSale,
								poster_type: PType,
								poster_level: PLevel
							}
						});
					})
				}
			},
			async bindClspCode(options) {
				if (!options.query) return;
				let clspId =
					options.query.clspId ||
					strToParams(decodeURIComponent(options.query.scene)).clspId
				if (clspId) {
					Cache.set('clsp_id', clspId);
					apiCloudShopBindCode({
						clsp_id: clspId,
					}).finally(() => {
						if (this.$Route.path != '/bundle2/pages/goods_detail/goods_detail')
							this.$Router.replaceAll({
								path: '/bundle2/pages/cloud_shop/diyshop/index',
								query: {
									clspId
								}
							})
					});
				}
			},
		},
	};
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "styles/common.scss";
	@import "plugin/emoji-awesome/css/apple.css";
	@import "components/uview-ui/index.scss";
	@import "styles/def_iconfont.css";

	.jishuzhichi-nodata {
		position: fixed;
		bottom: calc(constant(safe-area-inset-bottom));
		bottom: calc(env(safe-area-inset-bottom));
		width: 100%;
		margin: 0 auto;
	}
</style>